<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * class Page_model
 * @author Davinder
 * @version 1.0
 */
 
class Page_model extends CI_Model
{
    private $_tables;
	private $_debug_mode = FALSE;
    const PAGE_SIZE = 100;
	
	public function __construct() {
        parent::__construct();
		
		$this->_tables = new stdClass();
        $this->_tables->pages = 'yb_scrapm_pages';
        $this->_tables->pictures = 'yb_scrapm_pictures';
        $this->_tables->comments = 'yb_scrapm_comments';
        $this->_tables->likes = 'yb_scrapm_picture_likes';
        $this->_tables->users = 'yb_scrapm_users';
        $this->_tables->spam = 'yb_scrapm_spam';
		
		// Load DB if its enabled
		if ( config_item('rest_database_group') )
		{
			$this->db = $this->load->database(config_item('rest_database_group'), TRUE);
		}
	}
    
    public function get_active_pages($p_data=null) {
        
        if ( $p_data->disable_paging == FALSE ) {
            
            if ( empty($p_data->page_size) ) {
                $page_size = self::PAGE_SIZE;
            } else {
                $page_size = $p_data->page_size;
            }
            
            $page = ($p_data->page_index - 1) * $page_size;
		    $this->db->limit(($page_size+1),$page);
		}
        
        $this->db->where('page_status','active');
        $query = $this->db->get($this->_tables->pages);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
        
            $data = array();
            
            $data['more'] = 0;
            $i = 0;
            $resultSet = $query->result();
            
            foreach ( $resultSet as $row ) {
                
                if ( $p_data->disable_paging == FALSE && $i > ($page_size-1) && $i == (count($resultSet)-1) ) {
                    break;
                }
                
                $data['resultSet'][$i] = new stdClass();
                
                $data['resultSet'][$i]->id = $row->page_id;
                $data['resultSet'][$i]->name = $row->page_name;
                $data['resultSet'][$i]->desc = $row->page_desc;
                $data['resultSet'][$i]->school_id = $row->page_school_id;
                $data['resultSet'][$i]->academic_session = $row->page_academic_year;
                
                $i++;
            }
            
            if ( $p_data->disable_paging == FALSE && $this->_debug_mode == TRUE ) {
                log_message('DEBUG',__METHOD__.': Condition count(resultSet) < $page_size =>'.count($resultSet).' > '.$page_size);
            }
            
            if ( $p_data->disable_paging == FALSE && count($resultSet) > $page_size ) {
                $data['more'] = 1;
            }
            
            return $data;
        } else {
            return FALSE;
        }
    }
    
    public function get_page_details($p_data=null) {
        
        $this->db->where('page_id',$p_data->page_id);
        $this->db->where('page_status','active');
        $query = $this->db->get($this->_tables->pages);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            
            $row = $query->row();
            $data = new stdClass();
            
            $data->id = $row->page_id;
            $data->name = $row->page_name;
            $data->desc = $row->page_desc;
            $data->school_id = $row->page_school_id;
            $data->academic_session = $row->page_academic_year;
            
            return $data;
        } else {
            return FALSE;
        }
    }
    
    public function get_page_images($p_data=null,$p_users=null) {
        
        $show_page_id_flag = FALSE;
        
        if ( is_array($p_data) || is_object($p_data) ) {
            $p_data = (array) $p_data;
            $show_page_id_flag = TRUE;
            $this->db->where_in('picture_page_id',$p_data);
        } else {
            $this->db->where('picture_page_id',$p_data->page_id);
        }
        
        if ( !empty($p_users) ) {
            $this->db->where_in('picture_created_by',$p_users);
        }
        
        $this->db->where('picture_status','active');
        $this->db->order_by('picture_created_datetime','desc');
        $query = $this->db->get($this->_tables->pictures);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            
            $i = 0;
            $resultSet = $query->result();
            
            foreach ( $resultSet as $row ) {
                
                $data['resultSet'][$i] = new stdClass();
                
                $data['resultSet'][$i]->id = $row->picture_id;
                $data['resultSet'][$i]->created_by = $row->picture_created_by;
                $data['resultSet'][$i]->created_datetime = $row->picture_created_datetime;
                $data['resultSet'][$i]->thumbnail = $row->picture_thumbnail;
                $data['resultSet'][$i]->fullimage = $row->picture_fullimage;
                $data['resultSet'][$i]->mimetype = $row->picture_mimetype;
                $data['resultSet'][$i]->intermediate = $row->picture_intermediate;
                $data['resultSet'][$i]->story = $row->picture_story;
                $data['resultSet'][$i]->caption = $row->picture_caption;
                
                if ( $show_page_id_flag == TRUE ) {
                    $data['resultSet'][$i]->page_id = (int) $row->picture_page_id;
                }
                
                $i++;
            }
            
            return $data;
        } else {
            return FALSE;
        }
    }
    
    public function get_page_images_by_user($p_data=null) {
        
        if ( $p_data->disable_paging == FALSE ) {
            
            if ( empty($p_data->page_size) ) {
                $page_size = self::PAGE_SIZE;
            } else {
                $page_size = $p_data->page_size;
            }
            
            $page = ($p_data->page_index - 1) * $page_size;
		    $this->db->limit(($page_size+1),$page);
		}
        
        $this->db->where('picture_created_by',$p_data->user_id);
        $this->db->where('picture_status','active');
        $this->db->order_by('picture_created_datetime','desc');
        $query = $this->db->get($this->_tables->pictures);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            
            $data = array();
            
            $data['more'] = 0;
            $i = 0;
            $resultSet = $query->result();
            
            foreach ( $resultSet as $row ) {
                
                if ( $p_data->disable_paging == FALSE && $i > ($page_size-1) && $i == (count($resultSet)-1) ) {
                    break;
                }
                
                $data['resultSet'][$i] = new stdClass();
                
                $data['resultSet'][$i]->id = $row->picture_id;
                $data['resultSet'][$i]->created_by = $row->picture_created_by;
                $data['resultSet'][$i]->created_datetime = $row->picture_created_datetime;
                $data['resultSet'][$i]->thumbnail = $row->picture_thumbnail;
                $data['resultSet'][$i]->fullimage = $row->picture_fullimage;
                $data['resultSet'][$i]->mimetype = $row->picture_mimetype;
                $data['resultSet'][$i]->intermediate = $row->picture_intermediate;
                $data['resultSet'][$i]->story = $row->picture_story;
                $data['resultSet'][$i]->caption = $row->picture_caption;
                
                $i++;
            }
            
            if ( $p_data->disable_paging == FALSE && $this->_debug_mode == TRUE ) {
                log_message('DEBUG',__METHOD__.': Condition count(resultSet) < $page_size =>'.count($resultSet).' > '.$page_size);
            }
            
            if ( $p_data->disable_paging == FALSE && count($resultSet) > $page_size ) {
                $data['more'] = 1;
            }
            
            return $data;
        } else {
            return FALSE;
        }
    }
    
    public function get_picture_comments($p_data) {
        
        if ( $p_data->disable_paging == FALSE ) {
            
            if ( empty($p_data->page_index) ) {
                $p_data->page_index = 1;
            }
            
            if ( empty($p_data->page_size) ) {
                $page_size = self::PAGE_SIZE;
            } else {
                $page_size = $p_data->page_size;
            }
            
            $page = ($p_data->page_index - 1) * $page_size;
		    $this->db->limit(($page_size+1),$page);
		}
        
        $picture_id = $p_data->id;
        
        $this->db->select("yb_scrapm_comments.*,CONCAT_WS(' ',yb_scrapm_users.firstname,yb_scrapm_users.lastname ) as name,yb_scrapm_users.profile_image_url");
        $this->db->where('comment_picture_id',$picture_id);
        $this->db->where('comment_status','active');
        $this->db->from($this->_tables->comments);
        $this->db->join($this->_tables->users,'yb_scrapm_comments.comment_created_by = yb_scrapm_users.id','left');
        $this->db->order_by('comment_created_datetime','desc');
        $query = $this->db->get();
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            
            $data = array();
            
            $data['more'] = 0;
            $i = 0;
            $resultSet = $query->result();
            
            foreach ( $resultSet as $row ) {
                
                if ( $p_data->disable_paging == FALSE && $i > ($page_size-1) && $i == (count($resultSet)-1) ) {
                    break;
                }
                
                $data['resultSet'][$i] = new stdClass();
                
                $data['resultSet'][$i]->id = $row->comment_id;
                $data['resultSet'][$i]->comment_text = $row->comment_text;
                $data['resultSet'][$i]->comment_created_by = $row->comment_created_by;
                $data['resultSet'][$i]->comment_created_datetime = $row->comment_created_datetime;
                $data['resultSet'][$i]->comment_creator_name = $row->name;
                $data['resultSet'][$i]->comment_creator_image = $row->profile_image_url;
                
                $i++;
            }
            
            if ( $p_data->disable_paging == FALSE && $this->_debug_mode == TRUE ) {
                log_message('DEBUG',__METHOD__.': Condition count(resultSet) < $page_size =>'.count($resultSet).' > '.$page_size);
            }
            
            if ( $p_data->disable_paging == FALSE && count($resultSet) > $page_size ) {
                $data['more'] = 1;
            }
            return $data;
        } else {
            return FALSE;
        }
        
    }
    
    public function get_picture_likes($p_data) {
        
        $picture_id = $p_data->id;
        
        $this->db->select("yb_scrapm_picture_likes.*,CONCAT_WS(' ',yb_scrapm_users.firstname,yb_scrapm_users.lastname ) as name,yb_scrapm_users.profile_image_url");
        $this->db->where('like_picture_id',$picture_id);
        $this->db->where('like_is_active','active');
        $this->db->where('like_type','like');
        $this->db->from($this->_tables->likes);
        $this->db->join($this->_tables->users,'yb_scrapm_picture_likes.like_created_by = yb_scrapm_users.id','left');
        $this->db->order_by('like_created_datetime','desc');
        $query = $this->db->get();
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            
            $i = 0;
            $resultSet = $query->result();
            
            foreach ( $resultSet as $row ) {
                
                $data['resultSet'][$i] = new stdClass();
                
                $data['resultSet'][$i]->id = $row->like_id;
                $data['resultSet'][$i]->type = $row->like_type;
                $data['resultSet'][$i]->created_by = $row->like_created_by;
                $data['resultSet'][$i]->created_datetime = $row->like_created_datetime;
                $data['resultSet'][$i]->updated_datetime = $row->like_updated_datetime;
                $data['resultSet'][$i]->like_creator_name = $row->name;
                $data['resultSet'][$i]->like_creator_image = $row->profile_image_url;
                
                $i++;
            }
            
            return $data;
        } else {
            return FALSE;
        }
        
    }
    
    public function upload_picture($p_data) {
        
        $data = new stdClass();
        
        $this->db->set('picture_created_datetime','now()',FALSE);
        $data->picture_page_id = $p_data->page_id;
        $data->picture_status = 'active';
        $data->picture_created_by = $p_data->user_id;
        
        $data->picture_thumbnail = '';
        $data->picture_fullimage = '';
        $data->picture_mimetype = $p_data->uploaded_file_data['file_ext'];
        $data->picture_intermediate = '';
        $data->picture_story = $p_data->story;
        $data->picture_caption = $p_data->caption;
        
        $query_status = $this->db->insert($this->_tables->pictures,$data);
        
        if ($query_status) {
            $picture_id = $this->db->insert_id();
            return $picture_id;
        } else {
            return FALSE;
        }
    }
    
    public function move_and_update_picture($p_data) {
        
        $data = new stdClass();
        
        $this->db->set('picture_created_datetime','now()',FALSE);
        
        $data->picture_thumbnail = $p_data->thumbnail;
        $data->picture_fullimage = $p_data->fullimage;
        $data->picture_intermediate = $p_data->intermediate;
        
        $this->db->where('picture_id',$p_data->picture_id);
        $query_status = $this->db->update($this->_tables->pictures,$data);
        
        if ($query_status) {
            return true;
        } else {
            return FALSE;
        }
    }
    
    public function post_comment($p_data) {
        
        $data = new stdClass();
			
        $data->comment_picture_id = $p_data->picture_id;
        $data->comment_text = $p_data->picture_comment;
        $data->comment_created_by = $p_data->user_id;
        $data->comment_status = 'active';
        
        $this->db->set('comment_created_datetime','now()',FALSE);
        $query_status = $this->db->insert($this->_tables->comments,$data);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }
        
        if ( $query_status ) {
            return array('comment_id'=>$this->db->insert_id());
        } else {
            return FALSE;
        }
    }
    
    public function post_like($p_data) {
        
        $data = new stdClass();
        
        $operation_insert = TRUE;
		
        if ( !empty($p_data->like_id) ) {
            $this->db->where('like_id',$p_data->like_id);    
            $operation_insert = FALSE;
        }
        	
        $data->like_picture_id = $p_data->picture_id;
        $data->like_type = $p_data->like_type;
        $data->like_created_by = $p_data->user_id;
        $data->like_is_active = 'active';
        
        if ( $operation_insert == TRUE ) {
            $this->db->set('like_created_datetime','now()',FALSE);
            $query_status = $this->db->insert($this->_tables->likes,$data);
            $like_id = $this->db->insert_id();
        } else {
            $this->db->set('like_updated_datetime','now()',FALSE);
            $query_status = $this->db->update($this->_tables->likes,$data);
            $like_id = $p_data->like_id;
        }
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }
        
        if ( $query_status ) {
            return array('like_id'=>$like_id);
        } else {
            return FALSE;
        }
    }
    
    public function check_page_already_exists($p_data) {
        
        //$school_id = $p_data->school_id;
        $page_name = $p_data->page_name;
        $academic_id = $p_data->academic_id;
        
        $this->db->like('page_name',$page_name);
        //$this->db->where('page_school_id',$school_id);
        $this->db->where('page_academic_year',$academic_id);
        $query = $this->db->get($this->_tables->pages);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            return TRUE;
        } else {
            return FALSE;
        }
        
    }
    
    public function create_page($p_data) {
        
        $data = new stdClass();
			
        $data->page_school_id = null;//$p_data->school_id;
        $data->page_name = $p_data->page_name;
        $data->page_desc = $p_data->page_desc;
        $data->page_academic_year = $p_data->academic_id;
        $data->page_created_by = $p_data->user_id;
        $data->page_status = 'active';
        
        $this->db->set('page_created_datetime','now()',FALSE);
        $query_status = $this->db->insert($this->_tables->pages,$data);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }
        
        if ( $query_status ) {
            return array('page_id'=>$this->db->insert_id());
        } else {
            return FALSE;
        }        
    }
    
    public function post_spam($p_data) {
        
        $data = new stdClass();
			
        $data->spam_type_id = $p_data->spam_id;
        $data->spam_type = $p_data->spam_type;
        $data->spam_reason = $p_data->spam_reason;
        $data->spam_reported_by = $p_data->user_id;
        
        $this->db->set('spam_report_datetime','now()',FALSE);
        $query_status = $this->db->insert($this->_tables->spam,$data);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }
        
        if ( $query_status ) {
            return array('spam_id'=>$this->db->insert_id());
        } else {
            return FALSE;
        }
    }
    
    public function spam_details($p_data) {
        
        $picture_id = $p_data->id;
        
        $this->db->select("yb_scrapm_spam.*,CONCAT_WS(' ',yb_scrapm_users.firstname,yb_scrapm_users.lastname ) as name,yb_scrapm_users.profile_image_url");
        $this->db->where('spam_type_id',$picture_id);
        $this->db->where('spam_type','picture');
        $this->db->from($this->_tables->spam);
        $this->db->join($this->_tables->users,'yb_scrapm_spam.spam_reported_by = yb_scrapm_users.id','left');
        $this->db->order_by('spam_report_datetime','desc');
        $query = $this->db->get();
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            
            $resultSet = $query->result();
            
            $i = 0;
            
            foreach ( $resultSet as $row ) {
                
                $data['resultSet'][$i] = new stdClass();
                
                $data['resultSet'][$i]->reason = $row->spam_reason;
                $data['resultSet'][$i]->reported_by = $row->spam_reported_by;
                $data['resultSet'][$i]->spam_datetime = $row->spam_report_datetime;
                $data['resultSet'][$i]->spam_reporter_name = $row->name;
                $data['resultSet'][$i]->spam_reporter_image = $row->profile_image_url;
                
                $i++;
            }
            
            return $data;
        } else {
            return FALSE;
        }
    }
    
    public function get_school_picture_details($p_data=null) {
        
        //$this->db->where('page_school_id',$p_data->school_id);
        $this->db->where('page_academic_year',$p_data->academic_id);
        $this->db->where('page_status','active');
        $query = $this->db->get($this->_tables->pages);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            
            $i = 0;
            $resultSet = $query->result();
            
            foreach ( $resultSet as $row ) {
                
                $data['resultSet'][$i] = new stdClass();
                
                $data['resultSet'][$i]->id = $row->page_id;
                $data['resultSet'][$i]->name = $row->page_name;
                
                $i++;
            }
            
            return $data;
        } else {
            return FALSE;
        }
    }
    
    
    public function get_images_by_page($p_data=null) {
        
        $this->db->select('firstname, lastname, picture_id, picture_thumbnail, picture_mimetype, picture_fullimage, picture_intermediate, picture_story, picture_caption');
        $this->db->from($this->_tables->pictures);
        $this->db->join($this->_tables->pages, 'yb_scrapm_pages.page_id = yb_scrapm_pictures.picture_page_id');
        $this->db->join($this->_tables->users, 'yb_scrapm_users.id = yb_scrapm_pictures.picture_created_by');
                
        if (!empty($p_data->page_id))
        {
            $this->db->where('page_id', $p_data->page_id);
        }
        
        if (!empty($p_data->page_name))
        {
            $this->db->where('page_name', $p_data->page_name);
        }
        
        $this->db->where('picture_status','active');
        
        $this->db->order_by('picture_created_datetime');
        $query = $this->db->get();
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            
            $data = array();
            
            $i = 0;
            $resultSet = $query->result();
            
            foreach ( $resultSet as $row ) {
                
                $data['resultSet'][$i] = new stdClass();
                
                $data['resultSet'][$i]->picture_id = $row->picture_id;
                $data['resultSet'][$i]->thumbnail = $row->picture_thumbnail;
                $data['resultSet'][$i]->fullimage = $row->picture_fullimage;
                $data['resultSet'][$i]->mimetype = $row->picture_mimetype;
                $data['resultSet'][$i]->intermediate = $row->picture_intermediate;
                $data['resultSet'][$i]->story = $row->picture_story;
                $data['resultSet'][$i]->caption = $row->picture_caption;
                $data['resultSet'][$i]->firstname = $row->firstname;
                $data['resultSet'][$i]->lastname = $row->lastname;
                
                $i++;
            }
            
            return $data;
        } else {
            return FALSE;
        }
    }
}